package IO;

import IO.ImageManager;

import java.io.IOException;
import java.util.Scanner;
public class Main {
    static ImageManager imagemanager=new ImageManager();
    public static void main(String[] args) throws IOException {
        int key=0;
        printfMenu();

        while (key !=5) {
            key=chooseMenu();
            switch (key) {
                case 1:
                    downloadImages();
                    break;
                case 2:
                    imagemanager.listImages();
                    break;
                case 3:
                    searchImages();
                    break;
                case 4:
                    printfMenu();
                default:
                    break;
            }
        }
    }

    public static void printfMenu(){
        System.out.println("=== 图片下载系统 ===");
        System.out.println("1. 下载图片");
        System.out.println("2. 查看所有图片");
        System.out.println("3. 搜索图片");
        System.out.println("4. 打印菜单");
        System.out.println("5. 退出\n");
    }

    public static int chooseMenu(){
        // 创建 Scanner 对象，用于读取用户输入
        int num=0;
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入你选择使用的功能：");
        if (scanner.hasNextInt()) {
            num = scanner.nextInt(); // 读取一行字符串
            if (1 <= num && num <= 5) {
                return num;
            } else {
                System.out.println("请输入正确的数字序号");
                return -1;
            }
        } else{
            System.out.println("请输入正确的数字序号");
            return -1;
        }
    }

    public static void downloadImages() throws IOException {
        Scanner scanner1 = new Scanner(System.in);
        System.out.print("请输入你要下载的图片的地址：");
        String URL = scanner1.nextLine(); // 读取一行字符串
        imagemanager.downloadImage(URL);
    }

    public static void searchImages(){
        Scanner scanner2 = new Scanner(System.in);
        System.out.print("请输入你要查找的图片的名字：");
        String name = scanner2.nextLine(); // 读取一行字符串
        imagemanager.searchImage(name);
    }
}